package com.jyx.manage_course.service;

import com.jyx.framework.domain.course.CourseMarket;
import com.jyx.framework.domain.course.response.CourseCode;
import com.jyx.framework.model.response.CommonCode;
import com.jyx.framework.model.response.ResponseResult;
import com.jyx.manage_course.dao.CourseMarketRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CourseMarketService {
    @Autowired
    CourseMarketRepository courseMarketRepository;

    public CourseMarket getCourseMarketById(String courseId) {
        Optional<CourseMarket> courseMarketOptional = courseMarketRepository.findById(courseId);
        if(courseMarketOptional.isPresent()){
            CourseMarket courseMarket = courseMarketOptional.get();
            return courseMarket;
        }
        return null;
    }

    public ResponseResult updateCourseMarketById(String id, CourseMarket courseMarket) {
        CourseMarket courseMarketById = getCourseMarketById(id);
        if(courseMarketById != null){
            courseMarketById.setPrice(courseMarket.getPrice());
            courseMarketById.setStartTime(courseMarket.getStartTime());
            courseMarketById.setEndTime(courseMarket.getEndTime());
            courseMarketById.setQq(courseMarket.getQq());
            courseMarketById.setValid(courseMarket.getValid());
            courseMarketById.setCharge(courseMarket.getCharge());
            CourseMarket save = courseMarketRepository.save(courseMarketById);
            if(save != null){
                return new ResponseResult(CommonCode.SUCCESS);
            }
        }else {
            CourseMarket save = courseMarketRepository.save(courseMarket);
            if(save != null){
                return new ResponseResult(CommonCode.SUCCESS);
            }
        }
        return new ResponseResult(CourseCode.COURSE_COURSEMARKETISNULL);
    }
}
